/**
 * @project readnote
 * @Title BookAction.java
 * @author huxh
 * @date 2012-2-12
 **/
package com.readnote.center;

import java.io.File;
import java.util.Collection;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.cr.backbone.action.AppBaseActionSupport;
import com.cr.backbone.model.SysFile;
import com.readnote.user.model.User;
import com.readnote.user.model.UserInfo;
import com.readnote.user.service.UserInfoService;

public class UserInfoAction extends AppBaseActionSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1179031234511250745L;
	
	private static final String INDEX = "index";
	private static final String INPUT = "input";
	private static final String SUCCESS = "success";
	
	private UserInfo userInfo;
	private Collection<UserInfo> userInfos;
	private UserInfoService userInfoService;
	
	
	/***
	 * action method
	 */
	public String index(){
		Long userId=getLoginInfo().getUser().getId();
		User user=new User(userId);
		
		userInfo = userInfoService.saveUserInfoIfNotExitFromUser(user);
		return INDEX;
	}
	
	public String input(){
		
		if(userInfo!=null && !userInfo.isNullRow())
			userInfo = userInfoService.get(userInfo.getId());
		return INPUT;
	}
	
	public String save(){
		try{
			saveSysFile();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			
		}
		
		
		
		if(userInfo.isNullRow()){
			userInfoService.save(userInfo);
		}else{
			UserInfo _ui = userInfoService.get( userInfo.getId() );
			_ui.setRealName(userInfo.getRealName());
			_ui.setEmail(userInfo.getEmail());
			
			userInfoService.update(_ui);
		}
		return SUCCESS;
	}
	
	
	
	
	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public Collection<UserInfo> getUserInfos() {
		userInfos =userInfoService.getList();
		return userInfos;
	}

	public void setUserInfos(List<UserInfo> userInfos) {
		this.userInfos = userInfos;
	}

	public SysFile saveSysFile() throws Exception{
		//执行文件上传
		saveFileUpload();
		
		//生成系统上传文件记录
		SysFile sf= new SysFile();
		return sf;
	}
	
	private File upload;
	private String uploadFileName;
	private String uploadContentType;
	private FileUtils fileUtils;
	
	/**
	 * 文件上传功能
	 * @throws Exception
	 */
	public void saveFileUpload() throws Exception{
		if(upload ==null)
			return ;
		File srcFile=upload;
		String realPath  = ServletActionContext.getServletContext().getRealPath("uploadFile");
//		File destFile =new File(,uploadFileName);
		
		File destFile=new File(new File(realPath),uploadFileName);
		if(!destFile.getParentFile().exists())
			destFile.getParentFile().mkdirs();
		fileUtils.copyFile(srcFile, destFile);
		System.out.println("upload File Url:"+destFile.getPath());
		
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public FileUtils getFileUtils() {
		return fileUtils;
	}

	public void setFileUtils(FileUtils fileUtils) {
		this.fileUtils = fileUtils;
	}
	
}
